<?php

namespace App\Kuafu\Order\Command;

use App\Kuafu\Order\Model\SeparateTimeLogModel;
use Illuminate\Console\Command;

class ClearSeparateTimeLogCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:clear-separate-time-log';

    /**
     * The console command description.
     *
     * @var string
     */

    protected $description = '清理分表时间日志';

    /**
     * Execute the console command
     *
     * @return
     */
    public function handle()
    {
        // 保留3天数据，历史数据都进clickhouse了
        $startTime = strtotime(date('Ymd')) - 86400 * 3;

        $lastId = (int)SeparateTimeLogModel::query()
                                           ->where('createdAt', '>=', $startTime)
                                           ->orderBy('id')
                                           ->limit(1)
                                           ->value('id');

        if (!$lastId) {
            return;
        }

        $delRows = 10000;

        while (true) {
            $rows = SeparateTimeLogModel::query()->where('id', '<', $lastId)->limit($delRows)->delete();
            if ($rows < $delRows) {
                break;
            }
        }
    }

}
